<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>div水平垂直居中</title>
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    /*方法一*/
    
    .parent1 {
        width: 500px;
        height: 200px;
        border: 4px solid #FF4500;
        position: relative;
        margin-top: 20px;
        margin-bottom: 20px;
    }
    
    .child1 {
        width: 200px;
        height: 150px;
        margin: auto;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-color: #008B8B;
        line-height: 150px;
        text-align: center;
        color: white;
    }
    /*方法二*/
    
    .parent2 {
        width: 500px;
        height: 200px;
        border: 4px solid #008B8B;
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
    
    .child2 {
        width: 200px;
        height: 150px;
        display: inline-block;
        background-color: #008B8B;
        line-height: 150px;
        color: white;
    }
    /*方法三*/
    
    .parent3 {
        width: 500px;
        height: 200px;
        border: 4px solid #008B8B;
        margin-top: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .child3 {
        width: 200px;
        height: 150px;
        background-color: #008B8B;
        line-height: 150px;
        text-align: center;
        color: white;
    }
    /*方法四 */
    
    .parent4 {
        width: 500px;
        height: 200px;
        border: 4px solid #FF4500;
        position: relative;
        margin-top: 20px;
    }
    
    .child4 {
        width: 200px;
        height: 150px;
        background-color: #008B8B;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -100px;
        margin-top: -75px;
        line-height: 150px;
        text-align: center;
        color: white;
    }
    </style>
</head>

<body>
    <div class="parent1">
        <div class="child1">方法一</div>
    </div>
    <div class="parent2">
        <div class="child2">方法二</div>
    </div>
    <div class="parent3">
        <div class="child3">方法三</div>
    </div>
    <div class="parent4">
        <div class="child4">方法四</div>
    </div>
</body>

</html>
